<template>
  <el-dialog
    :title="title==='edit' ? '修改商品': '新增商品'"
    :visible="dialogVisible"
    @close="handleClose"
  >
    <el-form ref="form" label-width="140px" :rules="rules" :model="form">
      <el-form-item label="商品名称：" prop="skuName">
        <el-input
          v-model="form.skuName"
          type="text"
          style="width:396px"
          show-word-limit
          placeholder="请输入"
          maxlength="15"
          :validate-event="false"
        />
      </el-form-item>
      <el-form-item label="品牌：" prop="brandName">
        <el-input
          v-model="form.brandName"
          type="text"
          style="width:396px"
          show-word-limit
          placeholder="请输入"
          maxlength="10"
          :validate-event="false"
        />
      </el-form-item>
      <el-form-item label="商品价格(元)：" prop="price">
        <el-input-number v-model="form.price" controls-position="right" style="width:396px" placeholder="请输入" :validate-event="false" :precision="2" :step="0.5" :min="0.5" />
      </el-form-item>
      <el-form-item label="商品类型：" prop="classId">
        <el-select v-model="form.classId" placeholder="请选择" style="width:396px" :validate-event="false">
          <el-option v-for="item in typeList" :key="item.classId" :label="item.className" :value="item.classId" />
        </el-select>
      </el-form-item>
      <el-form-item label="规格：" prop="unit">
        <el-input
          v-model="form.unit"
          type="text"
          style="width:396px"
          show-word-limit
          placeholder="请输入"
          maxlength="10"
          :validate-event="false"
        />
      </el-form-item>
      <el-form-item label="商品图片：" prop="skuImage">
        <!-- <el-upload
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
        >
          <img class="avatar">
          <i class="el-icon-upload2" />
        </el-upload> -->
        <div style="font-size:14px;color:#bac0cd;" class="el-upload__tip"> 支持扩展名：jpg、png，文件不得大于100kb</div>
        <upLoading v-model="form.skuImage" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button style="background-color: #fbf4f0!important;color: #655b56!important;border:0;width:80px;height:36px;padding:0" @click="handleClose">取 消</el-button>
      <el-button type="primary" style="background: linear-gradient(135deg,#ff9743,#ff5e20)!important;border:0;width:80px;height:36px;padding:0;margin:0 0 0 34px" @click="submitData">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import upLoading from './uploading.vue'
import { getGoodsTypeInfo, addGoodsInfo, changeGoodsInfo } from '@/api/goods'
export default {
  name: 'AddGoods',
  components: {
    upLoading
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: null
    },
    title: {
      type: String,
      required: true
    },
    detail: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      form: {
        skuName: null,
        brandName: null,
        price: undefined,
        classId: null,
        unit: null,
        skuImage: null,
        skuId: null
      },
      rules: {
        skuName: [{ required: true, message: '请输入' }],
        brandName: [{ required: true, message: '请输入' }],
        price: [{ required: true, message: '请输入', trigger: 'change' }],
        classId: [{ required: true, message: '请输入' }],
        unit: [{ required: true, message: '请输入' }],
        skuImage: [{ required: true, message: '请上传' }]
      },
      typeList: []
    }
  },
  created() {
    this.renderTypeList()
  },
  methods: {
    handleClose() {
      // 重置表单
      this.$emit('handleClose', false)
      this.$refs.form.resetFields()
    },
    async renderTypeList() {
      const res = await getGoodsTypeInfo()
      console.log(res)
      this.typeList = res.currentPageRecords
    },
    async submitData() {
      try {
        await this.$refs.form.validate()
        if (this.title === 'edit') {
          await changeGoodsInfo(this.form)
          this.$message.success('修改成功')
        } else {
          const res = await addGoodsInfo(this.form)
          console.log(res)
          this.$message.success('添加成功')
        }
        this.handleClose()
        this.$emit('initTableData')
      } catch (error) {
        console.log(error)
      }
    },
    changeInfo() {
      console.log('子弹框信息', this.detail)
      const { skuName, brandName, price, skuClass, unit, skuImage, skuId } = this.detail
      this.form.skuName = skuName
      this.form.brandName = brandName
      this.form.price = price
      this.form.classId = skuClass.classId
      this.form.unit = unit
      this.form.skuImage = skuImage
      this.form.skuId = skuId
    }
  }
}
</script>

<style scoped lang='less'>
/deep/.el-dialog{
    border-radius: 10px;
    width: 630px;
    .el-dialog__header{
        .el-dialog__title{
            font-weight:700;
        }
    }
    .el-dialog__body{
    padding: 20px 20px 30px;
    text-align: center;
     .el-form{
       .el-form-item{
        text-align: left;
        .avatar-uploader{
          background-color: #f3f6fb;
          width:76px;
          height:76px;
          border:1px dashed #cccccc;
          border-radius: 5px;
          text-align: center;
          line-height: 76px;
        }
         label{
                height: 36px;
                font-size: 14px;
                font-weight: normal;
                color: #606266;
            }
        .el-form-item__content{
            .el-input{
                .el-input__inner{
                    height: 36px;
                }
            }
        }
        }
     }
    }
    .el-dialog__footer{
      text-align: center;
    }
}

</style>
